package com.email.service.impl;

import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.stereotype.Service;

import com.email.entity.Message;
import com.email.service.MessagePushSevice;

import wechat.app.WCCorpApplication;
import wechat.app.WXCorpAppFactory;
import wechat.base.snd.WCNews;
import wechat.base.snd.WCNewsItem;

@Service
public class WechatMessagePushSeviceImpl implements MessagePushSevice{

    public String send(final Message message){
		
	    WCCorpApplication _createCorpApp = WXCorpAppFactory.createCorpApp(
	    		System.getenv("CORP_ID"),System.getenv("SECRET"),System.getenv("AGENT_ID"));
	    final Procedure1<WCCorpApplication> _function = new Procedure1<WCCorpApplication>() {
	      public void apply(final WCCorpApplication it) {
	        WCNews _createNews = it.createNews();
	        final Procedure1<WCNews> _function = new Procedure1<WCNews>() {
	          public void apply(final WCNews it) {
	            WCNewsItem _withItem = it.withItem(message.getBody().getSubject());
	            final Procedure1<WCNewsItem> _function = new Procedure1<WCNewsItem>() {
	              public void apply(final WCNewsItem it) {
//	                it.setPicurl("http://58.247.87.218:8090/sample.jpg");
	                it.setUrl(message.getBody().getUrl());
	              }
	            };
	            ObjectExtensions.<WCNewsItem>operator_doubleArrow(_withItem, _function);
	            it.send2Users(CollectionLiterals.<String>newArrayList(message.getUserId()));
	          }
	        };
	        ObjectExtensions.<WCNews>operator_doubleArrow(_createNews, _function);
	      }
	    };
	    ObjectExtensions.<WCCorpApplication>operator_doubleArrow(_createCorpApp, _function);
	    
	    return "send success";
    }
}
